<?php
namespace Admin\Controller;

class IndexController extends AppController {

    public function index(){
        if(is_numeric('23')){
            echo 2342;
        }
        $this->show();
    }
    // 注册
    public function register()
    {
        if (IS_POST) {
            if (! empty($_POST)) {
                $dw_danweiDB = M('dw_danwei');
                $mobile = $this->_post('mobile');
                $yzm = $this->_post('yzm');
                $pwd = $this->_post('password');
                $q_pwd = $this->_post('q_password');
                $result= $this->isMobile($mobile);
                $mobile_yzm = $_SESSION['mobile_yzm'];
                if(!$result){
                    parent::outPutError('请输入正确的手机号');
                }
                if($pwd!=$q_pwd){
                    parent::outPutError('密码两次输入不一致');
                }

                if($yzm!=$mobile_yzm){
                    parent::outPutError('手机验证码输入错误');
                }
                //id,dwid,wlg,dw_userid,dw_truename,dw_name,dw_password,dw_phone,hytp,dw_jb,dw_date,dw_ldate,dw_enddate
                $data['dw_phone'] = $mobile;
                $data['dw_password'] = $pwd;
                $data['dwid'] = 0;
                $data['id'] = 0;
                $data['wlg'] = 1;
                $data['dw_userid'] = $mobile;
                $data['dw_truename'] = '花木岗个人会员';
                $data['dw_name'] = '花木岗个人会员';
                $data['hytp'] = 2;
                $data['dw_jb'] = 1;
                $data['dw_date'] = date('Y-m-d H:i:s');
                $data['dw_ldate'] = date('Y-m-d H:i:s');
                $data['dw_enddate'] = date('Y-m-d H:i:s');
                $id = $dw_danweiDB->add($data);
                if($id){
                    $dw_danweiDB->where("autoid=$id")->save(array('id'=>$id,'dwid'=>$id));
                    exit(json_encode(array('msg'=>'添加成功')));
                }else{
                    exit(json_encode(array('error'=>array('msg'=>'服务器繁忙，请稍后再试'))));
                }
            }
        }
    }

    public function mobile_send(){//手机验证码发送
        $mobile = $this->_post('mobile');
        $result = $this->isMobile($mobile);
        if($result){
            $yzm = rand(10000, 99999);
            $_SESSION['mobile_yzm'] = $yzm;
            //$content = "您的验证码为：$yzm，请在5分钟内填写。如非本人操作，请忽略！【花木港】";
            $re = SmsModel::sends($mobile, $yzm);
            if($re){
                exit(json_encode(array('msg'=>'短信发送成功')));
            }else{
                exit(json_encode(array('msg'=>'服务器繁忙，请稍后再试')));
            }

            // parent::outPutData(array('msg'=>'短信发送成功'));
        }else{
            exit(json_encode(array('msg'=>'','error'=>array('msg'=>'请填写正确的电话号码'))));
            // exit(json_encode(array('msg'=>'请填写正确的电话号码')));
            //parent::outPutError('请填写正确的电话号码');
        }
    }
    /**
     * 验证手机号是否正确
     * @author
     * @param INT $mobile
     */
    function isMobile($mobile) {
        if (!is_numeric($mobile)) {
            return false;
        }
        return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
    }

    // 登录
    public function login()
    {
        $dw_phone = I('dw_phone');

        $dw_password = I('dw_password');
        $db = M('dw_danwei');
        $info = $db->where(array('dw_phone' => $dw_phone, 'dw_password' => $dw_password))->find();
        if($info){
            $_SESSION['dwid'] = $info['dwid'];
            exit(json_encode(array('msg'=>'登录成功','id'=>$info)));
        }else{
            exit(json_encode(array('msg'=>'登录失败','error'=>array('msg'=>'用户名或者密码不正确'))));
        }

    }

    // 找回密码
    public function find_Pwd()
    {
        $obj = M('dw_danwei');
        $pwd = $this->_post('password');
        $p_pwd = $this->_post('p_password');
        $yzm = $this->_post('yzm');
        $mobile_yzm = $_SESSION['mobile_yzm'];
        $mobile = $this->_post('mobile');
        if($pwd!=$p_pwd){
            exit(json_encode(array('error'=>array('msg'=>'密码输入不一致'))));
        }
        if($yzm!=$mobile_yzm){
            exit(json_encode(array('error'=>array('msg'=>'验证码输入不正确'))));
        }

        $id = $obj->where("dw_phone='$mobile'")->save(array('dw_password'=>$pwd));
        if($id){
            exit(json_encode(array('msg'=>'密码修改成功')));
        }else{
            exit(json_encode(array('error'=>array('msg'=>'服务器繁忙，请稍后再试'))));
        }
    }


}